package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.BillConsumeDetailsEntity;
import cn.mumzone.pos.model.BillInfoListBean;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.ReturnGoodsEntity;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;

/**
 * Created by JoeLjt on 2019/7/9.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class ADA_BillReturnGoodsList extends CommonAdapter<ReturnGoodsEntity> {

    public ADA_BillReturnGoodsList(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_return_bill_list;
    }

    @Override
    protected void convert(ViewHolder holder, ReturnGoodsEntity goodsListBean, int position) {

        // 商品名
        TextView tvGoodsName = holder.getView(R.id.tv_goods_name);
        tvGoodsName.setText(addGoodsItemLabel(goodsListBean, tvGoodsName));

        // 规格
        holder.setText(R.id.tv_goods_spec, getGoodSpecString(goodsListBean.getGoodsSpec(), goodsListBean.getUnitName()));

        // 购买数量
        holder.setText(R.id.tv_goods_num, String.format("购买数量：%s", goodsListBean.getGoodsNum()));

        // 金额
        holder.setText(R.id.tv_goods_price, StringUtils.getFormattedMoneyWithUnit(goodsListBean.getPrice()));
        // 角标
        holder.setVisible(R.id.tv_select_count, goodsListBean.getSelectCount() != 0);
        holder.setText(R.id.tv_select_count, StringUtils.get(goodsListBean.getSelectCount()));


    }

    private SpannableStringBuilder addGoodsItemLabel(ReturnGoodsEntity entity, TextView textView) {

        StringSpanUtils utils = StringSpanUtils.init().addStr(entity.getGoodsName());

        if (StringUtils.isNumber(entity.getBargainingPrice()) && Double.parseDouble(entity.getBargainingPrice()) > 0) {
            utils.addRoundRectLabelWithRadius("议价", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }
        if (StringUtils.isNumber(entity.getSecondPrice()) && Double.parseDouble(entity.getSecondPrice()) > 0) {
            utils.addRoundRectLabelWithRadius("变价", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }
        if (entity.getPresentNum() > 0) {
            utils.addRoundRectLabelWithRadius("赠送(" + entity.getPresentNum() + ")", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }
        if (StringUtils.isTrue(entity.getIsConsign()) && entity.getConsignNum() > 0) {
            utils.addRoundRectLabelWithRadius("寄存(" + entity.getConsignNum() + ")", textView,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_service),
                    1, 1, 1, 0, true);
        }

        return utils.build();
    }

    private String getGoodSpecString(List<ReturnGoodsEntity.GoodsSpecBean> goodsSpec, String goodsUnit) {
        StringBuilder builder = new StringBuilder();
        builder.append("规格：");
        if (!ListUtil.isListEmpty(goodsSpec) || !TextUtils.isEmpty(goodsUnit)) {
            for (int i = 0; i < goodsSpec.size(); i++) {
                ReturnGoodsEntity.GoodsSpecBean goodsSpecBean = goodsSpec.get(i);
                builder.append(goodsSpecBean.getValue());
            }
            builder.append(goodsUnit);
        } else {
            builder.append("暂无");
        }
        return builder.toString();
    }

}
